package com.zrrd.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Employee;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Pageable;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //获取前端传递的用户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();

        //将密码进行mds加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //创建条件构造器
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        //执行查询
        Employee emp = employeeService.getOne(wrapper);
        //判断用户是否存在
        if (emp == null){
            return R.error("用户不存在");
        }
        //比对密码
            if (!emp.getPassword().equals(password)){
                return R.error("密码错误");
            }
            //判断用户状态
            if (emp.getStatus()==0){
                return R.error("用户被禁用");
            }
            //走到这里代表登录成功 将当前登录用户的ID 存入Session
            request.getSession().setAttribute("emp",emp.getId());
       //将登录员工的对象信息返回  不是登录成功而是emp
        return R.success(emp);
    }

    //登出
    @PostMapping("/logout")
    public  R<String> logout(HttpServletRequest request){
        //删除用户登录成功时候存入Session中的数据
        request.getSession().removeAttribute("emp");
        return R.success("退出成功");
    }


    //分页
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);


        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }


    //添加
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工，员工信息：{}", employee.toString());

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empId = (Long) request.getSession().getAttribute("emp");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }



    //修改员工状态
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){

        return employeeService.updateById(employee) == true ? R.success("更新成功"):R.error("更新失败");
    }

    //根据id获取员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id ){
        return R.success(employeeService.getById(id));
    }




}
